#!/usr/bin/perl


###	This script is part of the Zeitcrawler v1.3 (http://code.google.com/p/zeitcrawler/).
###	Copyright (C) Adrien Barbaresi 2011-2013.
###	This is free software, released under the GNU GPL v3 license (http://www.gnu.org/licenses/gpl.html).

# Function: expects a file named 'ZEIT_flatfile' and converts it to a series of XML documents on a 'one file per text' basis.
# The files are stored in a subdirectory which must be created before (the default is 'texts').
# Use: without arguments.

# Benefits : compatibility with other software to complete a further analysis of the texts.
# For example the textometry software TXM : http://txm.sourceforge.net/


use strict;
use warnings;

my ($text, $output);
my $counter = 0;
my $subdir = "texts"; ## change subdirectory here

# file name here:
my $input = "ZEIT_flatfile";

open (INPUT, "<", $input) or die "Can't open $input: $!";

while (<INPUT>) {
	$text .= $_;
	if ($_ =~ m/^-----$/) {
		$text = xmlize($text);
		$text =~ s/Titel: (.*?)\nExcerpt: (.*?)\nAutor: (.*?)\nDatum: (.*?)\nurl: (.*?)\n\n(.+?)-----/<text titel="$1" excerpt="$2" autor="$3" datum="$4" url="$5">\n<rohtext>\n$6<\/rohtext>\n<\/text>/s;

		$counter++;
		$output = $subdir . "/ZEIT_text-" . $counter . ".xml";
		open (OUTPUT, ">", $output) or die "Can't open $output: $!";
		print OUTPUT "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
		print OUTPUT "<!--File generated by the zeitcrawler : http://code.google.com/p/zeitcrawler/-->\n";
		print OUTPUT "<!DOCTYPE text [
  <!ELEMENT text (rohtext)>
  <!ATTLIST text titel CDATA #REQUIRED>
  <!ATTLIST text excerpt CDATA #REQUIRED>
  <!ATTLIST text autor CDATA #REQUIRED>
  <!ATTLIST text datum CDATA #REQUIRED>
  <!ATTLIST text url CDATA #REQUIRED>
  <!ELEMENT rohtext (#PCDATA)>
]>\n\n";
		print OUTPUT $text;
		$text = ();
		close(OUTPUT);
	}
}

close(INPUT);

sub xmlize {
	my $string = shift;
	$string =~ s/& /&amp;/g;
	$string =~ s/'/&apos;/g;
	$string =~ s/"/&quot;/g;
	return $string;
}
